package edu.qhst.user.listener;

import edu.qhst.common.utils.JsonUtils;
import edu.qhst.user.constant.MailConsts;
import edu.qhst.user.dao.entity.MailInfo;
import edu.qhst.user.service.MailService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


/**
 * 处理发送邮件 消费者
 *
 * @Author 加浓美式
 * @Data 2023/6/24
 */
@Component
@RequiredArgsConstructor
public class MailSendListener {
    private final MailService mailService;

    @RabbitListener(bindings =
    @QueueBinding(
            value = @Queue(value = "qhst.mail.queue",durable = "true"), //临时队列
            exchange = @Exchange(name = "mailExchange",type = "topic"), //指定交换机名称与类型
            key = {"user.mail"}
    )
    )
    private void sendMail(String mailDate) {
        MailInfo mailInfo = JsonUtils.parse(mailDate, MailInfo.class);
        if(mailInfo.getType() == 1) {
            mailService.sendHtmlMail(mailInfo.getMail(), "启航社团加入申请回复", MailConsts.getOKMailMsg(mailInfo.getName()));
        } else {
            mailService.sendHtmlMail(mailInfo.getMail(), "启航社团加入申请回复", MailConsts.getFailMailMsg(mailInfo.getName(), mailInfo.getMessage()));
        }
    }
}
